package com.sn.net;

import com.sn.bean.Address;
import com.sn.constant.Common;
import com.sn.constant.Windows;
import com.sn.frame.ClientFrame;
import lombok.Getter;
import lombok.Setter;

import java.io.IOException;
import java.net.*;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;

/**
 * @Description:
 * @Author: songbiao
 */
@Getter
@Setter
public class Client {

    private static final Object LOCK = new Object();
    private static DatagramSocket socket;
    private ExecutorService executorService = Executors.newFixedThreadPool(60);
    private static ClientFrame clientFrame;
    private Address default_address;

    public Client(String address) {
        default_address = new Address(address, Common.client_port);
    }

    /**
     * 启动客户端
     */
    public ClientFrame start(){
        clientFrame = new ClientFrame();
        return clientFrame;
    }

    public boolean send(String serverIP, int serverPort){
        //将ip端口发送过去
        String address = default_address.getHost()+ ":" + default_address.getPort();
        byte[] data = address.getBytes();
        try {
            // 创建socket并绑定端口
            socket = new DatagramSocket(Common.client_port);
            DatagramPacket packet = new DatagramPacket(data,0,data.length, InetAddress.getByName(serverIP),serverPort);
            // 向服务器端发送数据报
            socket.send(packet);
        } catch (IOException e) {
            System.err.println("发送失败");
        }
        return true;
    }

    public void accept(){
        while (true) {
            byte[] data = new byte[4* Windows.COMPRESS_HEIGHT *Windows.COMPRESS_WIDTH];
            try {
                synchronized (LOCK) {
                    //每次接收60kb
                    DatagramPacket packet = null;
                    int offset = 0;
                    int length = Common.TRANSFER_DATA;
                    while(true) {
                        packet = new DatagramPacket(data, offset, length);
                        socket.receive(packet);
                        byte[] end = new byte[]{data[offset],data[offset+1],data[offset+2]};
                        if (new String(end).equals("end")){
                            System.arraycopy(data,0,data,0,offset-1);
                            break;//完成一次接收
                        }
                        offset += length;
                    }
                    executorService.execute(new Runnable() {
                        @Override
                        public void run() {
                            clientFrame.draw(data);
                        }
                    });
                }
            } catch (IOException e) {
                System.err.println("接收数据失败...正在重新接收");
                try {
                    TimeUnit.MILLISECONDS.sleep(500);
                } catch (InterruptedException ex) {
                    ex.printStackTrace();
                }
                accept();
            }
        }
    }

}
